package com.news.news.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.news.news.dao.LinkDao;
import com.news.news.dao.NewsDao;
import com.news.news.modle.Link;
import com.news.news.modle.News;
import com.news.news.modle.NewsType;
import com.news.news.util.JWTUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;

@Service
@Transactional
public class LinkService {
    @Resource
    LinkDao linkDao;


    public PageInfo<Link> LinkList(@RequestBody Link link) {
        PageHelper.startPage(link.getPageNum(),link.getPageSize());
        ArrayList<Link> linkArrayList = linkDao.linkList(link);
//        System.out.println(roles);
        PageInfo<Link> pageInfo = new PageInfo<>(linkArrayList);
        return pageInfo;
    }

    public void saveLink(Link link, String token) {
        link.setAdminid(JWTUtil.getTokenInfo(token).getClaim("id").asInt());
        //保存信息并返回ID
        linkDao.saveLink(link);
    }

    public void UpdateLinkState(Link link) {
        linkDao.UpdateLinkState(link);
    }

    public Link findLinkById(Integer id) {
        Link link = linkDao.findLinkById(id);
        return link;
    }

    public void updateLink(Link link, String token) {
        link.setAdminid(JWTUtil.getTokenInfo(token).getClaim("id").asInt());
        //修改链接信息
        linkDao.updateLink(link);
    }
    //
    public void deleteLinkById(Integer id) {
        linkDao.deleteLinkById(id);
    }
    //
    //public ArrayList<NewsType> findNewsTypeList() {
    //    return newsDao.findNewsTypeList();
    //}
    //
}
